import {
	categoryList,categoryCreate,deleteCategory,amendCategory
} from '@/commonApi'
import pub from '@/lib'
const {getTreeData} = pub

export default {
	state: () => ({
		list:[]
	}),
	mutations: {
		getTreeData(state,list){
			state.list = list
		}
	},
	actions: {
		userList(context) {
			return categoryList()
				.then((data) => {
					const {list} = data
					getTreeData(list)
					.then((res)=>{
						context.commit('getTreeData',res)
					})
				})
		},
		addCategory({dispatch},category){
			return categoryCreate(category)
			.then(()=>{
				dispatch('userList')
			})
		},
		deleteCategory({dispatch},classId){
			return deleteCategory(classId)
			.then(()=>{
				dispatch('userList')
			})
		},
		amendCategory({dispatch},params){
			return amendCategory(params)
			.then(()=>{
				dispatch('userList')
			})
		}
	}
}
